import pika
import time

connection = pika.BlockingConnection(pika.ConnectionParameters("127.0.0.1"))
channel = connection.channel()
channel.queue_declare(queue='rpc_queue')

def fib(n):
    time.sleep(5)
    return n*n

def on_request(ch,method,props,body):
    n = int(body)
    print("[.] fib",n)
    response = fib(n)

    ch.basic_publish(exchange='',routing_key=props.reply_to,
                     properties=pika.BasicProperties(correlation_id=props.correlation_id),
                     body=str(response))
    ch.basic_ack(delivery_tag=method.delivery_tag)

channel.basic_consume(on_message_callback=on_request,queue='rpc_queue')
print("[*] Awaiting RPC requests")
channel.start_consuming()